import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useUserStore = defineStore(
  'user',
  () => {
    //全局状态-state

    // ref<null | string> 通过泛型指定了ref的类型可能是null或者string
    const username = ref<null | string>(null)
    const token = ref<null | string>(null)
    const role = ref<null | number>(null)
    const permissionList = ref<null | number>(null)
    // 动态菜单数据
    const menuList = ref<null | object[]>(null)

    // 用户的按钮权限类别
    const permissionBtnList = ref<null | string[]>(null)

    // 修改全局状态的函数--action-动作
    function setUsername(un) {
      username.value = un
    }
    function setToken(tk) {
      token.value = tk
    }
    function setRole(r) {
      role.value = r
    }
    function setPermissionList(r) {
      permissionList.value = r
    }
    function setMenuList(r) {
      menuList.value = r
    }

    function setPermissionBtnList(r) {
      permissionBtnList.value = r
    }

    // 导出
    return {
      username,
      token,
      setUsername,
      setToken,
      setRole,
      role,
      permissionList,
      setPermissionList,
      menuList,
      setMenuList,
      permissionBtnList,
      setPermissionBtnList,
    }
  },
  //设置这个store使用 本地保存
  {
    persist: true,
  },
)
